package com.neo.office.server.controller.system;

import com.neo.office.server.config.data.Result;
import com.neo.office.server.domain.entity.Job;
import com.neo.office.server.service.JobService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/system/basic/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @ApiOperation(value = "获取所有职称")
    @GetMapping("/")
    public List<Job> getAllJob(){
        return jobService.list();
    }

    @ApiOperation(value = "添加职称信息")
    @PostMapping("/")
    public Result addPosition(@RequestBody Job job){
        job.setCreateDate(LocalDateTime.now());
        if (jobService.save(job)) {
            return Result.success("添加成功！");
        }
        return Result.error("添加失败！");
    }

    @ApiOperation(value = "更新职称信息")
    @PutMapping("/")
    public Result updatePosition(@RequestBody Job job){
        if (jobService.updateById(job)) {
            return Result.success("更新成功！");
        }
        return Result.error("更新失败！");
    }

    @ApiOperation(value = "删除职称信息")
    @DeleteMapping("/{id}")
    public Result deletePosition(@PathVariable Integer id){
        if (jobService.removeById(id)) {
            return Result.success("删除成功！");
        }
        return Result.error("删除失败！");
    }

    @ApiOperation(value = "批量删除职称")
    @RequestMapping(method = RequestMethod.DELETE, value = "/")
    public Result deleteJobLevelByIds( Integer[] ids) {
        if (jobService.removeByIds(Arrays.asList(ids))) {
            return Result.success("删除成功！");
        }
        return Result.error("删除失败！");
    }

}
